package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

func main() {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	s1 := make([]int, 0, 100)
	for i := 0; i < 100; i++ {
		s1 = append(s1, (r.Intn(200) - 100))
	}
	sort.Sort(sort.Reverse(sort.IntSlice(s1)))
	fmt.Printf("%v,len=%d,cap=%d\n", s1, len(s1), cap(s1))

	//打印数字的重复次数
	//遍历切片，创建Map，字典的key为切片的元素，字典值为key得重复次数。
	m := make(map[int]int, 100)
	for t := 0; t < 100; t++ {
		m[s1[t]] = m[s1[t]] + 1
	}
	//遍历map，打印key和value
	fmt.Printf("%+v\n", "~~~~~~~~~~~~~~~~~~~~~~~~")

	var s2 []int
	for k := range m {
		s2 = append(s2, k)
	}
	sort.Sort(sort.Reverse(sort.IntSlice(s2)))
	for _, v := range s2 {
		fmt.Printf("%+v:%d ", v, m[v])
	}
}

// 批改意见
// 1. Intn(200)的范围是[0, 200)，会漏掉最后一个值